package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import entity.Publish;
import util.db.DBhelp;
import util.db.Page;
import util.db.RowMapper;

public class PublishDao {

	DBhelp<Publish> dBhelp = new DBhelp<Publish>();
	
	
	
	
	private class publishRowMapper implements RowMapper<Publish>{

		public Publish mapRow(ResultSet resultSet) throws SQLException {
			Publish publish = new Publish();
			publish.setId(resultSet.getString("id"));
			publish.setPublishname(resultSet.getString("publishname"));
			publish.setCreatetime(resultSet.getString("createtime"));
			return publish;
		}
		
	}


	public int findCount(){
		String sql = "select count(*) from t_publish";
		return dBhelp.executeQuery(sql).intValue();
	}

	public Page<Publish> findAllPublish(int nowpagenum) {
		Page<Publish> page = new Page<Publish>(nowpagenum, findCount());
		String sql = "select id,publishname,createtime from t_publish order by createtime desc limit ?,?";
		
		List<Publish> list = dBhelp.executeQueryForList(sql, new publishRowMapper(), page.getStartPage(),page.getPagesize());
		
		page.setList(list);
		return page;
		
	}

	public Publish findPublishByName(String publishName) {
		String sql = "select * from t_publish where publishname=?";
		return dBhelp.executeQueryToObject(sql, new publishRowMapper(), publishName);
	}

	public void savePublish(String id, String publishName) {
		String sql = "insert into t_publish (id,publishname)values(?,?)";
		dBhelp.update(sql, id,publishName);
		
	}

	public void update(Publish publish) {
		String sql = "update t_publish set publishname = ? where id = ?";
		dBhelp.update(sql, publish.getPublishname(),publish.getId());
		
	}

	public Publish findPublishById(String id) {
		String sql = "select * from t_publish where id = ?";
		return dBhelp.executeQueryToObject(sql, new publishRowMapper(), id);
	}

	public void delPublish(String id) {
		String sql = "delete from t_publish where id=?";
		dBhelp.update(sql, id);
		
	}

	public List<Publish> findAll() {
		String sql = "select * from t_publish ";
		return dBhelp.executeQueryForList(sql, new publishRowMapper());
	}
	
	
	
	
}
